package com.example.myapplication.provider;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.IndexImage;
import com.example.myapplication.utils.LoadNetImageUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class IndexImgPageSliderProvider extends PageSliderProvider {

    //将图片列表和页面传递过来
    private List<IndexImage> list;
    private AbilitySlice abilitySlice;

    public IndexImgPageSliderProvider(List<IndexImage> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        //返回渲染总数
        return list.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        //1.在布局文件中创建轮播图模板
        //2.获取轮播图模板
        DirectionalLayout template = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_index_image_template,null,false);
        //获取轮播图图片组件
        Image image = (Image) template.findComponentById(ResourceTable.Id_index_image);
        //获取轮播图数据,此对象中存储的是图片的网络地址
        IndexImage indexImage = list.get(i);
        //设置完整图片访问地址(自己更改)
        String imgUrl = indexImage.getImgurl(); //"XXX.png/jpg" ---完整地址：http://0.0.0.0/XXX
        String netImageUrl = "http://0.0.0.0/imgs/" + imgUrl;

        //渲染数据:如何将一个网络图片绑定到鸿蒙应用的组件中？（网络图片载入）
        //封装工具类
        //参数：页面 图片网络地址 图片组件（动态）
        LoadNetImageUtil.loadImg(abilitySlice,netImageUrl,image);
        //静态
        //image.setPixelMap(ResourceTable.Media_L1);

        //将图片模板绑定到pageSlider
        componentContainer.addComponent(template);

        return template;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }
}
